
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>8.读写分离 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="多个vagrant.html" />
    
    
    <link rel="prev" href="mycat简单入门demo.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="linux_安装mysql.html">
            
                <a href="linux_安装mysql.html">
            
                    
                    1.CentOS7离线安装MySQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="mycat目录.html">
            
                <a href="mycat目录.html">
            
                    
                    2.mycat目录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="linux_安装mycat.html">
            
                <a href="linux_安装mycat.html">
            
                    
                    3.linux_安装mycat
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="mycat的端口.html">
            
                <a href="mycat的端口.html">
            
                    
                    3.mycat的端口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="linux切换语言.html">
            
                <a href="linux切换语言.html">
            
                    
                    4.linux切换语言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="vagrant_开发端口.html">
            
                <a href="vagrant_开发端口.html">
            
                    
                    5.vagrant_开发端口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="schema_xml_dataHost.html">
            
                <a href="schema_xml_dataHost.html">
            
                    
                    6.schema.xml dataHost
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="mycat简单入门demo.html">
            
                <a href="mycat简单入门demo.html">
            
                    
                    7.mycat简单入门demo
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10" data-path="读写分离.html">
            
                <a href="读写分离.html">
            
                    
                    8.读写分离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="多个vagrant.html">
            
                <a href="多个vagrant.html">
            
                    
                    9.多个vagrant
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="mycat_router.html">
            
                <a href="mycat_router.html">
            
                    
                    10.mycat_router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="mysql如何做到主从复制.html">
            
                <a href="mysql如何做到主从复制.html">
            
                    
                    11.mysql如何做到主从复制
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >8.读写分离</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x8BFB;&#x5199;&#x5206;&#x79BB;">&#x8BFB;&#x5199;&#x5206;&#x79BB;</h1>
<ol>
<li><p>&#x9700;&#x8981;&#x4F60;&#x6709;2&#x4E2A;&#x670D;&#x52A1;&#x5668;</p>
<pre><code>     &lt;writeHost host=&quot;hostM1&quot; url=&quot;workstation:3306&quot; user=&quot;root&quot;
         password=&quot;111111&quot;&gt;
         &lt;readHost host=&quot;hostS&quot; url=&quot;workstation:3316&quot; user=&quot;root&quot;
             password=&quot;111111&quot; /&gt;
     &lt;/writeHost&gt;

      balance=&quot;1&quot;  //&#x6240;&#x6709;&#x8BFB;&#x64CD;&#x4F5C;&#x90FD;&#x968F;&#x673A;&#x7684;&#x53D1;&#x9001;&#x5230;readHost&#x3002;

      writeType=&quot;0&quot; //writeType=&#x201C;0&#x201D;, &#x6240;&#x6709;&#x5199;&#x64CD;&#x4F5C;&#x90FD;&#x53D1;&#x9001;&#x5230;&#x53EF;&#x7528;&#x7684;writeHost&#x4E0A;&#x3002;

     explain create table company(id int not null primary key,name varchar(100));
             create table company(id int not null primary key,name varchar(100));
     explain insert into company(id, name) values (100, &apos;abc&apos;);
             insert into company(id, name) values (100, &apos;abc&apos;);
</code></pre></li>
<li>&#x5982;&#x4F55;&#x6D4B;&#x8BD5;&#x8BFB;&#x5199;fenl&#xFF0C;&#x65E5;&#x8BB0;&#xFF1F;
```
&#x5982;&#x679C;&#x60F3;&#x770B;&#x66F4;&#x591A;&#x7684;MyCAT&#x5904;&#x7406;&#x7EC6;&#x8282;&#xFF0C;&#x9700;&#x8981;&#x6253;&#x5F00;debug&#x7EA7;&#x522B;&#x7684;log&#xFF0C;&#x4FEE;&#x6539;log4j.xml&#xFF0C;</li>
</ol>
<root>
  <level value="debug">
  <appender-ref ref="FILE">
   <!--<appender-ref ref="FILE" />-->
</appender-ref></level></root>


<p>select * from company where id = 100;</p>
<p>cat mycat/logs/mycat.log</p>
<pre><code>

</code></pre><pre><code>  mycat
    /    \
</code></pre><p>  master -- slave (&#x4E3B;&#x4ECE;&#x590D;&#x5236;)</p>
<schema name="testdb" checksqlschema="false" sqlmaxlimit="100" datanode="dn1">

</schema>

<datanode name="dn1" datahost="vm3306" database="db1">
</datanode>

<p>balance=&quot;3&quot;&#xFF1A;&#x8868;&#x793A;&#x5199;&#x8BF7;&#x6C42;&#x53EA;&#x53D1;&#x7ED9;&#x8282;&#x70B9;&#xFF0C;&#x8BFB;&#x8BF7;&#x6C42;&#x53EA;&#x53D1;&#x7ED9;&#x8BFB;&#x8282;&#x70B9;&#x3002;</p>
<datahost name="vm3306" maxcon="1000" mincon="10" balance="3" writetype="0" dbtype="mysql" dbdriver="native"> <heartbeat>select user()</heartbeat> 
<!-- can have multi write hosts -->
 <writehost host="hostM1" url="192.168.0.120:3306" user="tuser" password="guli123"> 
    <!-- can have multi read hosts --> 
    <readhost host="hostS1" url="192.168.0.122:3306" user="tuser" password="guli123"> </readhost></writehost>
    <!-- <writeHost host="hostM2" url="localhost:3316" user="tuser" password="guli123"/> --> 
</datahost>


<p>writeType=&quot;0&quot;, &#x6240;&#x6709;&#x5199;&#x64CD;&#x4F5C;&#x53D1;&#x9001;&#x5230;&#x914D;&#x7F6E;&#x7684;&#x7B2C;&#x4E00;&#x4E2A; writeHost,&#x7B2C;&#x4E00;&#x4E2A;&#x6302;&#x4E86;&#x5207;&#x5230;&#x8FD8;&#x751F;&#x5B58;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A; writeHost,&#x91CD;&#x65B0;&#x542F;&#x52A8;&#x540E;&#x5DF2;&#x5207;&#x6362;&#x540E;&#x7684;&#x4E3A;&#x51C6;,&#x5207;&#x6362;&#x8BB0;&#x5F55;&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;:dnindex.properties .</p>
<p>balance=&quot;3&quot;,&#x6240;&#x6709;&#x8BFB;&#x8BF7;&#x6C42;&#x968F;&#x673A;&#x7684;&#x5206;&#x53D1;&#x5230; wiriterHost &#x5BF9;&#x5E94;&#x7684; readhost &#x6267;&#x884C;,writerHost &#x4E0D;&#x8D1F;&#x62C5;&#x8BFB;&#x538B;&#x529B;,&#x6CE8;&#x610F; balance=3 &#x53EA;&#x5728; 1.4 &#x53CA;&#x5176;&#x4EE5;&#x540E;&#x7248;&#x672C;&#x6709;,1.3 &#x6CA1;&#x6709;&#x3002;</p>
<p>&#x67E5;&#x770B;&#x65E5;&#x8BB0; tail -f wrapper.log</p>
<pre><code>
&#x4E09;&#x3001; mycat&#x8BFB;&#x5199;&#x5206;&#x79BB;&#x9A8C;&#x8BC1;
1&#x3001;&#x66F4;&#x6539;&#x65E5;&#x5FD7;&#x7684;&#x8F93;&#x51FA;&#x6A21;&#x5F0F;

vi log4j.xml
&#x4FEE;&#x6539;&lt;asyncRoot level=&quot;info&quot; includeLocation=&quot;true&quot;&gt;
&#x4E3A; &lt;asyncRoot level=&quot;debug&quot; includeLocation=&quot;true&quot;&gt;
2&#x3001;&#x521B;&#x5EFA;&#x8868;

# mysql -uroot -p123456 -h127.0.0.1 -P 8066
mysql&gt; create table test (id bigint not null primary key,user_id varchar(100),date DATE, fee decimal);

3&#x3001;&#x63D2;&#x5165;&#x6570;&#x636E;

mysql&gt; insert into test(id,user_id,date,fee)  values(1,@@hostname,20161201,100);
mysql&gt; insert into test(id,user_id,date,fee)  values(5000001,@@hostname,20161202,100);

4&#x3001;&#x67E5;&#x8BE2;&#x89C2;&#x5BDF;&#x65E5;&#x5FD7;&#x7684;&#x53D8;&#x5316;

mysql&gt; select * from test
tail -f wrapper.log
</code></pre><p>&#x6CE8;&#x610F;&#xFF1A;Mycat&#x4E3B;&#x4ECE;&#x5206;&#x79BB;&#x53EA;&#x662F;&#x5728;&#x8BFB;&#x7684;&#x65F6;&#x5019;&#x505A;&#x4E86;&#x5904;&#x7406;&#xFF0C;&#x5199;&#x5165;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EA;&#x4F1A;&#x5199;&#x5165;&#x5230;writehost&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;mycat&#x7684;&#x4E3B;&#x4ECE;&#x590D;&#x5236;&#x5C06;&#x6570;&#x636E;&#x590D;&#x5236;&#x5230;readhost&#xFF0C;
&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x5F53;&#x65F6;&#x5019;&#x6211;&#x7EA0;&#x7ED3;&#x4E86;&#x597D;&#x4E45;&#xFF0C;&#x6570;&#x636E;&#x5199;&#x5165;writehost&#x540E;&#xFF0C;readhost&#x4E00;&#x76F4;&#x6CA1;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x4EE5;&#x4E3A;&#x662F;&#x81EA;&#x5DF1;&#x914D;&#x7F6E;&#x7684;&#x95EE;&#x9898;&#xFF0C;
&#x540E;&#x9762;&#x624D;&#x53D1;&#x73B0;Mycat&#x5C31;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x4E3B;&#x4ECE;&#x590D;&#x5236;&#x7684;&#x529F;&#x80FD;&#xFF0C;
&#x6BD5;&#x7ADF;&#x6570;&#x636E;&#x5E93;&#x672C;&#x8EAB;&#x81EA;&#x5E26;&#x7684;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x624D;&#x662F;&#x6700;&#x9AD8;&#x6548;&#x7A33;&#x5B9A;&#x7684;&#x3002;
```</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="mycat简单入门demo.html" class="navigation navigation-prev " aria-label="Previous page: 7.mycat简单入门demo">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="多个vagrant.html" class="navigation navigation-next " aria-label="Next page: 9.多个vagrant">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"8.读写分离","level":"1.10","depth":1,"next":{"title":"9.多个vagrant","level":"1.11","depth":1,"path":"多个vagrant.md","ref":"./多个vagrant.md","articles":[]},"previous":{"title":"7.mycat简单入门demo","level":"1.9","depth":1,"path":"mycat简单入门demo.md","ref":"./mycat简单入门demo.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"读写分离.md","mtime":"2018-07-23T06:10:51.658Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-07-23T06:58:37.163Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

